var map, vectorLayer, drawController;
var WGS_PROJECTION = new OpenLayers.Projection("EPSG:4326");
var SPHERICAL_MERCATOR_PROJECTION = new OpenLayers.Projection("EPSG:900913");

function init() {
	
	map = new OpenLayers.Map("map", {
		displayProjection: WGS_PROJECTION,
		restrictedExtent: new OpenLayers.Bounds(116.27, 4.39, 127, 21.53).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION)
	});
    var osm = new OpenLayers.Layer.Google('Google Map Layer', {});

	var defaultStyle = new OpenLayers.Style({ 
		fillColor:  '#aaee77', 
		fillOpacity: .8, 
		pointRadius: 14, 
		strokeColor: '#669933', 
	});
	
	var drawnStyle = defaultStyle;
	
	var styleMap = new OpenLayers.StyleMap({ 
		'default': defaultStyle,
		'temporary': drawnStyle
	});
	
	vectorLayer = new OpenLayers.Layer.Vector("Reports");
	vectorLayer.styleMap = styleMap;
	
	map.addLayers([osm, vectorLayer]);
	
	var mouseDefaults =  new OpenLayers.Control.MouseDefaults({
		title:'Default mouse configuration'
	});
	
    map.addControl(new OpenLayers.Control.MousePosition());
	
	drawController = new OpenLayers.Control.DrawFeature(vectorLayer, OpenLayers.Handler.Point, {featureAdded: onFeatureAdded});
	map.addControl(drawController);
	
    map.setCenter(new OpenLayers.LonLat(121.06850, 14.64866).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION), 14);
	
}

function activateDrawFeature(){
	drawController.activate();
}

function onFeatureAdded(feature){
    vectorLayer.removeAllFeatures();
    vectorLayer.addFeatures(feature);
    mapPoint = new OpenLayers.Geometry.Point(feature.geometry.getBounds().getCenterLonLat().lon, feature.geometry.getBounds().getCenterLonLat().lat).transform(SPHERICAL_MERCATOR_PROJECTION, WGS_PROJECTION);
    lon = mapPoint.x;
    lat = mapPoint.y;
}
